package com.wframe.project.former.dataservices.marketing;

import com.frame.database.JBaseService;
import com.frame.database.JServiceFactory;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;

import java.util.List;

/**
 * Created by Author on 2017/6/3.
 */
public class BuySendService extends JBaseService<BuySendDao> {

    public static BuySendService getIntance() {
        return JServiceFactory.createJServiceProxy(BuySendService.class);
    }

    /**
     * 根据买送促销表的id查询一条买送促销的活动信息
     * @param buySendId
     * @return
     * @throws Exception
     */
    public JdataBean selectBuySendByid(int buySendId) throws Exception {
        return getDao().selectBuySendById(buySendId);
    }

    public PageResult queryChooseProduct(JdataBean parms, int size, int pageno) throws Exception {
        return getDao().queryChooseProduct(parms, size, pageno);
    }


    public void addControlsalesSingLe(String promotionid, JSONArray sales, String userids) throws Exception {
        getDao().deleteControlsalesAll(promotionid);
        String[] uids = null;
        if (StringUtils.isNotBlank(userids)) uids = userids.split(",");
        int iscontrol = 0;
        if (uids != null && uids.length > 0) iscontrol = 1;

        if (sales != null && sales.length() > 0){
            iscontrol = 1;
        }
        getDao().updatPromotecontrol(promotionid, iscontrol);//标记控销状态
        if (sales != null) {
            for (int i = 0; i < sales.length(); i++) {
                JdataBean bean = new JdataBean();
                bean.put("promotion_id", promotionid);
                bean.put("type", sales.getJSONObject(i).getString("type"));
                bean.put("val", sales.getJSONObject(i).getString("val"));
                bean.put("text", sales.getJSONObject(i).getString("text"));
                getDao().addControlsalesSingLe(bean);
            }
        }
        if (uids != null) {
            for (String uid : uids) {
                JdataBean bean = new JdataBean();
//                bean.put("productid", productid);
                bean.put("type", 4);
                bean.put("val", uid);
                getDao().addControlsalesSingLe(bean);
            }

        }
    }

    public JdataBean queryproduct(int id) throws Exception {
        return getDao().queryproduct(id);
    }
    public JdataBean querySelectedProduct(int id) throws Exception {
        return getDao().querySelectedProduct(id);
    }
}
